package com.kolon.business.mOI.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOIMySuggestionListController")
public class MOIMySuggestionListController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI/mOIMySuggestionList.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 회원정보 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
		int pageInt = ComUtil.sToi(page) * 15;
		String cpyCD = (String)userMap.get("CompanyCD");
		// XML DOCUMENT 및 인증 처리
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/oi_suggestion.aspx?strGubun=mylist&strMode=mysuggest&companycd=").append(cpyCD);
		buffer.append("&userid=").append(userId);
		buffer.append("&ViewRowCount=").append(pageInt).append("&Page=1");
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("SUG_My_List_T");
		Iterator itXml = xmlList.iterator();
		List mapList = new ArrayList();
		Map map = null; 
		// XML 데이터 (목록 정보)
		while (itXml.hasNext()) {
			map = new HashMap();
			Element el = (Element)itXml.next();
			map.put("nOrders", el.getChild("nOrders").getText());
			map.put("baseseq", el.getChild("baseseq").getText());
			map.put("ideabaseseq", el.getChild("ideabaseseq").getText());
			map.put("sugid ", el.getChild("sugid").getText());
			map.put("sugflag", el.getChild("sugflag").getText());
			map.put("sub1", el.getChild("sub1").getText());
			map.put("title", el.getChild("title").getText());
			map.put("createdate", el.getChild("createdate").getText());
			map.put("sflag", el.getChild("sflag").getText());
			map.put("deptname", el.getChild("deptname").getText());
			map.put("judgeResult", el.getChild("judgeResult").getText());
			map.put("statuscd", el.getChild("statuscd").getText());
			map.put("judge_flag", el.getChild("judge_flag").getText());
			map.put("themeseq", el.getChild("themeseq").getText());
			map.put("viewcnt", el.getChild("viewcnt").getText());
			map.put("exec_keepflag", el.getChild("exec_keepflag").getText());
			map.put("exec_baseseq", el.getChild("exec_baseseq").getText());
			map.put("exec_execempid", el.getChild("exec_execempid").getText());
			map.put("exec_empid", el.getChild("exec_empid").getText());
			map.put("exec_ideabaseseq", el.getChild("exec_ideabaseseq").getText());
			map.put("fOrders", el.getChild("fOrders").getText());
			map.put("CommentNum", el.getChild("CommentNum").getText());
			map.put("ModifyTitle", el.getChild("ModifyTitle").getText());
			map.put("companycd", el.getChild("companycd").getText());
			mapList.add(map);
		}
		// 목록에 대한 정보 (총수, 페이지, 현재페이지 등)
		Map tbMap = null;
		element = doc.getRootElement();
		xmlList = element.getChildren("TableInfo");
		itXml = xmlList.iterator();
		// XML 데이터 
		if (itXml.hasNext()) {
			tbMap = new HashMap();
			Element el = (Element)itXml.next();
			tbMap.put("TotalRecordCnt", el.getChild("TotalRecordCnt").getText());
			tbMap.put("TotalPage", el.getChild("TotalPage").getText());
			tbMap.put("CurrPage", el.getChild("CurrPage").getText());
			tbMap.put("OnePageCnt", el.getChild("OnePageCnt").getText());
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("tbMap", tbMap);
		modelAndView.addObject("page", page);
		return modelAndView;
	}

}
